package com.ve.clexer;

public class BraceMatcher {
    private int braceLevers[] = new int[4];
    private int complete;


    public static final int BRACE_NONE = -1;//()
    public static final int BRACE_LITTLE = 0;//()
    public static final int BRACE_MIDDLE = 1;//[]
    public static final int BRACE_BIG = 2;//{}
    public static final int BRACE_ANGLE = 3;//<>

    private static final char[][] BRACES = {
            {'(', ')'},
            {'[', ']'},
            {'{', '}'},
            {'<', '>'},
    };

    public boolean listen(int type, char c) {
        if (c == BRACES[type][0]) {
            braceLevers[type]++;
        } else if (c == BRACES[type][1]) {
            if (braceLevers[type] > 0) {
                braceLevers[type]--;
            } else {
                return true;
            }
        }
        return false;
    }

    public void reset() {

    }

    public void increase(int type) {
        braceLevers[type]++;
    }

    public boolean has(int type) {
        return braceLevers[type] > 0;
    }
    public boolean isComplete(int type){
        return complete ==type;
    }

    public void listen(char c) {
        for (int type = 0; type < braceLevers.length; type++) {
            if (listen(type,c)) {
                complete =type;
                return;
            }
        }
        complete =BRACE_NONE;
    }
}
